<template>
  <div class="app-container">

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      max-height="690"
      highlight-current-row
      style="width: 100%;"
      :row-style="{height:'32px'}"
      tooltip-effect="light"
      :default-sort="{prop: 'reportId', order: null}"
      @sort-change="sortChange"
    >
      <el-table-column label="编号" align="center" fixed width="150px" prop="reportId" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.reportId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发起者ID" align="center" prop="userId" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.userId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容类型" align="center" prop="reportType" sortable="custom">
        <template slot-scope="{row}">
          <el-tag v-if="row.reportType===1" size="mini" type="primary">帖子</el-tag>
          <el-tag v-else-if="row.reportType===2" size="mini" type="primary">回帖</el-tag>
          <el-tag v-else-if="row.reportType===3" size="mini" type="primary">评论</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="被举报内容" align="center">
        <template slot-scope="{row}">
          <el-button size="mini" @click="handlePreviewContent(row.reportType, row.businessId)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="举报理由" align="center" min-width="250px" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.reportReason }}</span>
        </template>
      </el-table-column>
      <el-table-column label="举报状态" align="center" prop="reportStatus" sortable="custom">
        <template slot-scope="{row}">
          <el-tag v-if="row.reportStatus===0" size="mini" type="danger">待处理</el-tag>
          <el-tag v-else-if="row.reportStatus===1" size="mini" type="success">已处理</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="处理结果" align="center" prop="reportResult" sortable="custom">
        <template slot-scope="{row}">
          <el-tag v-if="row.reportResult===0" size="mini" effect="dark" type="success">已忽略</el-tag>
          <el-tag v-else-if="row.reportResult===1" size="mini" effect="dark" type="danger">已处罚</el-tag>
          <el-tag v-else size="mini" type="info">等待处理</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="处理时间" align="center" prop="updateTime" sortable="custom">
        <template slot-scope="{row}">
          <span v-if="row.reportStatus===1">{{ row.updateTime }}</span>
          <span v-else>/</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="280px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button :disabled="row.reportStatus===1" type="primary" size="mini" @click="handleReport(row)">
            处理
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row.reportId)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-size.sync="listQuery.pageSize" @pagination="showReportList" />

    <el-dialog title="处理举报" :visible.sync="dialogVisible" width="600px">
      <el-form ref="dataForm" :model="temp" label-width="100px" style="width: 100%">
        <el-form-item label="处理结果" prop="result">
          <el-select v-model="temp.result" placeholder="请选择处理结果" clearable style="width: 250px;" class="filter-item">
            <el-option :key="0" label="忽略此次举报" :value="0" />
            <el-option :key="1" label="通过该举报，下线相关内容" :value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="doCheckReport">
          确认
        </el-button>
      </div>
    </el-dialog>
    <el-dialog title="查看被举报内容" :visible.sync="dialogContentVisible" width="600px">
      <div style="margin: 10px;">
        <el-row v-if="tempContent.title">
          <el-col :span="24" class="issue-title">
            {{ tempContent.title }}
          </el-col>
        </el-row>
        <el-row>
          <span class="middle-align"><el-image :key="tempContent.headImg" :src="tempContent.headImg" class="user-avatar" /></span>
          <span v-if="tempContent.realName" class="nickname middle-align">{{ tempContent.realName }}</span>
        </el-row>
        <el-row>
          <div v-if="tempContent.content" class="issue-content" v-html="tempContent.content" />
          <div v-else class="issue-content">[无内容]</div>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogContentVisible = false">
          关闭
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { showReportList, deleteReport, handleReport, getReportContent } from '@/api/discuss'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default {
  name: 'ForumReport',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      dialogLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        sortProp: '',
        sortOrder: ''
      },
      temp: {
        reportId: '',
        result: ''
      },
      tempContent: {
        reportType: '',
        userId: '',
        realName: '',
        headImg: '',
        title: '',
        content: ''
      },
      dialogVisible: false,
      dialogContentVisible: false
    }
  },
  created() {
    this.showReportList()
  },
  methods: {
    showReportList() {
      this.listLoading = true
      showReportList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.showReportList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.listQuery.sortProp = prop
      if (order === 'ascending') {
        this.listQuery.sortOrder = '1'
      } else {
        this.listQuery.sortOrder = '-1'
      }
      this.handleFilter()
    },
    resetDialog() {
      this.temp = {
        reportId: undefined,
        result: ''
      }
    },
    handleReport(row) {
      this.resetDialog()
      this.temp.reportId = row.reportId
      this.dialogVisible = true
    },
    doCheckReport() {
      if (this.temp.result === 0 || this.temp.result === 1) {
        handleReport(this.temp).then(() => {
          this.showReportList()
          this.dialogVisible = false
          this.$notify({
            title: 'Success',
            message: '处理成功',
            type: 'success',
            duration: 2000
          })
        })
      }
    },
    handleDelete(bulletinId) {
      this.$confirm('此操作将删除该举报记录，是否继续?', '提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteReport(bulletinId).then(() => {
          this.showReportList()
          this.dialogVisible = false
          this.$notify({
            title: 'Success',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    handlePreviewContent(reportType, businessId) {
      this.tempContent = {}
      this.dialogContentVisible = true
      getReportContent(reportType, businessId).then(response => {
        this.tempContent = Object.assign({}, response.data)
      })
    }
  }
}
</script>

<style scoped>
  .col-title {
    text-align: right
  }

  .col-title p {
    font-weight: bolder;
  }

  .col-content p {
    color: #848383
  }
  .user-avatar {
    cursor: pointer;
    width: 24px;
    height: 24px;
    border-radius: 12px;
  }
  .nickname {
    color: #475669;
    margin-left: 10px;
  }
  .middle-align {
    vertical-align: middle;
  }
  .issue-content {
    margin-top: 20px;
  }
  .issue-title {
    font-weight: bold;
    font-size: medium;
    margin-bottom: 15px;
  }
</style>
